package com.joplus.housekeeper.ui.fragment;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chatuidemo.ui.ChatActivityNew;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.ConvertisementAdapter;
import com.joplus.housekeeper.base.BaseFragment2;
import com.joplus.housekeeper.bean.MainBean;
import com.joplus.housekeeper.bean.MessageAccountListBean;
import com.joplus.housekeeper.bean.MessageRecordListBean;
import com.joplus.housekeeper.bean.RecordListBean;
import com.joplus.housekeeper.bean.XmppUser;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;
import com.joplus.housekeeper.ui.activity.ImSearchActivity;
import com.joplus.housekeeper.ui.view.LoadMoreListView;
import com.joplus.housekeeper.utils.RealmHelper;
import com.joplus.housekeeper.utils.RxBus;
import com.joplus.housekeeper.utils.T;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import io.reactivex.functions.Consumer;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
 * Created by zisheng on 2017/9/7.
 */

public class ConvertisementFragment extends BaseFragment2 {


    private PtrClassicFrameLayout ptr_frame;
    private LoadMoreListView lv1;
    private ConvertisementAdapter convertisementAdapter;
    private RealmResults<XmppUser> xmppUsers;
    private TextView et;


    @Override
    public View initView(LayoutInflater inflater) {

        return inflater.inflate(R.layout.fragment_convertisement, null);
    }

    @Override
    protected void initFindViewById(View view) {
        RxBus.getInstance().subscribe(MessageRecordListBean.class, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                if (AllmessageAccountList == null) {
                    return;
                }
                MessageRecordListBean bean = (MessageRecordListBean) o;
                for (int i = 0; i < AllmessageAccountList.size(); i++) {
                    if (AllmessageAccountList.get(i).getAccountId().equals(bean.getAccountId())) {
                        int unRead = AllmessageAccountList.get(i).getUnRead();
                        AllmessageAccountList.get(i).setUnRead(unRead++);
                        break;
                    } else if (i == AllmessageAccountList.size() - 1 && !AllmessageAccountList.get(i).getAccountId().equals(bean.getAccountId())) {
                        MessageAccountListBean messageAccountListBean = new MessageAccountListBean();
                        messageAccountListBean.setUnRead(1);
                        messageAccountListBean.setMessageDatetime(bean.getMessageDatetime());
                        messageAccountListBean.setAccountId(bean.getAccountId());
                        messageAccountListBean.setAvatar(bean.getAvatar());
                        messageAccountListBean.setClientType(bean.getClientType());
                        messageAccountListBean.setImId(bean.getMessageFrom());
                        messageAccountListBean.setLastMessageContent(bean.getContent());
                        messageAccountListBean.setName(bean.getName());
                        AllmessageAccountList.add(messageAccountListBean);
                    }
                }
            }
        });
        ptr_frame = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_frame);
        lv1 = (LoadMoreListView) view.findViewById(R.id.lv);
        convertisementAdapter = new ConvertisementAdapter(getActivity());
        lv1.setAdapter(convertisementAdapter);
        et = (TextView) view.findViewById(R.id.et);
        ptr_frame.setLastUpdateTimeRelateObject(this);
        ptr_frame.setLoadingMinTime(1000);
        ptr_frame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

                AllmessageAccountList.clear();
                startRow = 0;
                getUser();
            }
        });
        lv1.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                getUser();
            }
        });
        et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), ImSearchActivity.class);
                startActivity(intent);
            }
        });
    }

    private List<MessageAccountListBean> AllmessageAccountList = new ArrayList<>();
    int startRow = 0;

    private void getUser() {
        //   xmppUsers = RealmHelper.instance().allUser();
        RequestHelper.getInstance().imRecod(getContext(), startRow, 20, new TextCallback() {
            @Override
            public void onFailure(String responseString) {
                ptr_frame.refreshComplete();
            }

            @Override
            public void onSuccess(Object responseString) {
                ptr_frame.refreshComplete();
                MainBean bean = (MainBean) responseString;
                if (!bean.getCode().equals(getString(R.string.right_code))) {
                    T.showShort(getContext(), bean.getMessage());
                    return;
                }
                List<MessageAccountListBean> messageAccountList = bean.getData().getMessageAccountList();

                if (messageAccountList == null || messageAccountList.size() < 1) {
                    return;
                }
                if (startRow == bean.getData().getStartRow()) {
                    return;
                }
                AllmessageAccountList.addAll(messageAccountList);
                convertisementAdapter.setData(AllmessageAccountList);
                convertisementAdapter.notifyDataSetChanged();
                startRow = bean.getData().getStartRow();

            }
        });
    }

    @Override
    public void initData() {
        getUser();
    }


}
